<!--
  * 巡检线路
  *
-->
<template>


  <a-modal
      title="线路详情"
      width="600"
      :open="visibleFlag"
      @cancel="onClose"
      @Ok="handleOk"
      :maskClosable="false"
      :destroyOnClose="true"
      footer=""
  >
<div   v-show="type==0">

<a-button  type="primary" @click="add">添加</a-button>
  <a-table
      size="small"
      :dataSource="tableData"
      :columns="columns"
      rowKey="id"
      bordered
      @cancel="onClose"
      :pagination="false"
  >


    <template #bodyCell="{ text, record, column }" >
      <template  v-if="column.dataIndex === 'action'" >
        <div class="" >
          <a-button @click="editrow(record)" type="link">编辑</a-button>
          <a-button @click="deleterow (record)" danger type="link">删除</a-button>
        </div>
      </template>
    </template>
  </a-table>
</div>
<div v-show="type==1">


  <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 10 }" >

    <a-form-item label="名称"  name="name">
      <a-input style="width: 300px" v-model:value="form.name" placeholder="名称" />
    </a-form-item>



    <a-form-item label="经度"  name="longitude">
      <a-input style="width: 300px" v-model:value="form.longitude" placeholder="经度" type="number"  />
    </a-form-item>



    <a-form-item label="纬度"  name="longitude">
      <a-input style="width: 300px"  v-model:value="form.latitude" placeholder="纬度"  type="number" />
    </a-form-item>


  </a-form>

  <a-button  style="margin-left: 40%;margin-right: 10px" @click="handleCancel">返回</a-button>
  <a-button  style=""  type="primary" @click="save">保存</a-button>
</div>




  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import {message, Modal} from 'ant-design-vue';
  import { SmartLoading } from '/src/components/framework/smart-loading';
  import { gInspectionLinesApi } from '/@/api/business/xjgl/g-inspection-lines-api';
  import { smartSentry } from '/src/lib/smart-sentry';
   import DictSelect from '/src/components/support/dict-select/index.vue';
  import DepartmentTreeSelect from '/@/components/system/department-tree-select/index.vue';
  import {gInspectionLinesPointsApi} from "/@/api/business/xjgl/g-inspection-lines-points-api.js";


  const type = ref(0);
  const form=ref({});
  const tableData = ref([]);
  const columns = ref([
    {
      title: '名称',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '精度',
      dataIndex: 'longitude',
      ellipsis: true,
    },
    {
      title: '纬度',
      dataIndex: 'latitude',
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      ellipsis: true,
    }
    ])
  const departmentTreeSelect = ref();

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);
  const  lineId=ref("");
  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    console.log("rowData"+JSON.stringify(rowData))
    lineId.value=rowData.id;
    query(rowData.id)
    visibleFlag.value = true;
  /*  nextTick(() => {
      formRef.value.clearValidate();
    });*/
  }
  async function query(id){

    const queryResult =  await  gInspectionLinesApi.queryPointPage({id:id});

    console.log("queryResult:"+JSON.stringify(queryResult))

    tableData.value=queryResult.data;

  }
  function add(){

    const lineId =form.value.lineId;

     form.value={}

    type.value=1;

  }


  async function deleterow(val){

    Modal.confirm({
      title: '提示',
      content: '确定要删除选吗?',
      okText: '删除',
      okType: 'danger',
      async onOk() {
        await gInspectionLinesApi.deletePoint(val.id);
        message.success('操作成功');
        emits('reloadList');
        //onClose();
        query(lineId.value);
      },
      cancelText: '取消',
      onCancel() {},
    });
  }
  function editrow(val){
    type.value=1;
    console.log(val)
    form.value=val;

  }
  function onClose() {
    type.value=0;
 //   Object.assign(form, formDefault);
 //   visibleFlag.value = false;
    visibleFlag.value = false;
  }
  const handleOk = () => {
    visibleFlag.value = false;
  };

  const handleCancel = () => {
    type.value=0;
  };
  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
          id: undefined,
              name: undefined, //线路名称
              deptId: undefined, //组织机构
              remark: undefined, //备注
  };

  //let form = reactive({ ...formDefault });

  const rules = {
                  name: [{ required: true, message: '线路名称 必填' }],
                  deptId: [{ required: true, message: '组织机构 必填' }],
                longitude: [{ required: true, message: '经纬度 必填',type:'number'}],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    console.log(form.value)
    console.log("1221"+form.value.longitude)
    try {
      if (form.value.id) {
        await gInspectionLinesPointsApi.update(form.value);
      } else {

        console.log(lineId.value)
        form.value.lineId=lineId.value;
        await gInspectionLinesPointsApi.add(form.value);
      }
      message.success('操作成功');
      type.value=0;
      emits('reloadList');
  //    onClose();
      query( lineId.value);
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
